package cn.com.nou.base.service.bdm;

import cn.com.nou.base.dao.bdm.LearningFormMapper;
import cn.com.nou.common.builder.PaginationBuilder;
import cn.com.nou.common.entity.NouEnumerationT;
import cn.com.nou.common.entity.Pagination;
import cn.com.nou.common.utils.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LearningFormService {

    @Autowired
    private LearningFormMapper learningFormMapper;

    private final Log log = LogFactory.getLog(getClass());

    /**
     * 根据type取字典表数据列表列表分页
     * @param nouEnumerationT
     * @return
     */
    public Pagination getEnumByType(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            PaginationBuilder paginationBuilder = new PaginationBuilder();
            Pagination paginating = paginationBuilder.getPaginating(nouEnumerationT.getPageNo(), nouEnumerationT.getPageSize());
            paginationBuilder.setTotal(learningFormMapper.getEnumByTypeCount());
            paginationBuilder.setResultList(learningFormMapper.getEnumByType(nouEnumerationT,paginating));
            return paginationBuilder.build();
        } catch (Exception e) {
            log.debug("LearningFormService-->>getEnumByType",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据type添加字典表
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    public int addEnumByType(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            if (learningFormMapper.getSameLevelItemCodeOrItemName(nouEnumerationT).toArray().length!=0) {
                return 0;
            } else {
                if (!"learningForm".equals(nouEnumerationT.getType().toString())) {
                    return 0;
                }
                nouEnumerationT.setId(Utils.getUUID());
                nouEnumerationT.setTypeName("学习形式");
                nouEnumerationT.setIsDefault("0");
                learningFormMapper.addEnumByType(nouEnumerationT);
                return 1;
            }
        } catch (Exception e) {
            log.debug("LearningFormService-->>addEnumByType",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据ID更新字典
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    public int updateEnumById(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            if (!"learningForm".equals(nouEnumerationT.getType().toString())) {
                return 0;
            }
            return learningFormMapper.updateEnumById(nouEnumerationT);
        } catch (Exception e) {
            log.debug("LearningFormService-->>updateEnumById",e);
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据Id删除字典
     * @param nouEnumerationT
     * @return
     * @throws Exception
     */
    public int deleteEnumById(NouEnumerationT nouEnumerationT) throws Exception {
        try {
            int stu = learningFormMapper.getStuUsedEnumCount(nouEnumerationT);
            int sch = learningFormMapper.getUsedEnumCount(nouEnumerationT);
            if (stu == 0 && sch == 0) {
                return learningFormMapper.deleteEnumById(nouEnumerationT);
            } else {
                return 0;
            }
        } catch (Exception e) {
            log.debug("LearningFormService-->>deleteEnumById",e);
            throw new RuntimeException(e.getMessage());
        }
    }

}
